package com.chen.util;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.chen.model.Base;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;


public class ExcelUtil {

	/**
	 * @param args
	 */
	public boolean toexcel(List<Base> bases) throws Exception{
		//创建excel工作表
		WritableWorkbook book = Workbook.createWorkbook(new File("c://work.xls"));
		WritableSheet sheet = book.createSheet("one", 0);
		
		List<String> labelStr = new ArrayList<String>();
		labelStr.add("ID");
		labelStr.add("job_No");
		labelStr.add("name");
		labelStr.add("postion");
		labelStr.add("Bclass");
		labelStr.add("payFactor");
		labelStr.add("NH");
		labelStr.add("NP");
		labelStr.add("OH");
		labelStr.add("OP");
		labelStr.add("SH");
		labelStr.add("SP");
		labelStr.add("HH");
		labelStr.add("HP");
		labelStr.add("Bonus");
		labelStr.add("Gross");
		labelStr.add("NSSA");
		labelStr.add("Paye");
		labelStr.add("AIDS");
		labelStr.add("NEC");
		labelStr.add("Total");
		labelStr.add("NET");

		for(int i=0;i<labelStr.size();i++){
			sheet.addCell(new Label(i, 0, labelStr.get(i)));
		}
				
		Base base = new Base();
		for(int i=0;i<bases.size();i++){
			System.out.println(i+"-----------------------"+BaseUtil.beDouble(base.getnH()));
			 base = bases.get(i);
			 sheet.addCell(new Label(0,i+1,base.getJob_No()));
			 sheet.addCell(new Label(1,i+1,base.getJob_No2()));
			 sheet.addCell(new Label(2,i+1,base.getName()));
			 sheet.addCell(new Label(3,i+1,base.getPostion()));
			 sheet.addCell(new Label(4,i+1,base.getBclass()));
			 sheet.addCell(new jxl.write.Number(5,i+1,BaseUtil.beDouble(base.getPayFactor())));
			 sheet.addCell(new jxl.write.Number(6, i+1,BaseUtil.beDouble(base.getnH())));
			 sheet.addCell(new jxl.write.Number(7, i+1,BaseUtil.beDouble(base.getnP())));
			 sheet.addCell(new jxl.write.Number(8, i+1,BaseUtil.beDouble(base.getoH())));
			 sheet.addCell(new jxl.write.Number(9, i+1,BaseUtil.beDouble(base.getoP())));
			 sheet.addCell(new jxl.write.Number(10, i+1,BaseUtil.beDouble(base.getsH())));
			 sheet.addCell(new jxl.write.Number(11, i+1,BaseUtil.beDouble(base.getsP())));
			 sheet.addCell(new jxl.write.Number(12, i+1,BaseUtil.beDouble(base.gethH())));
			 sheet.addCell(new jxl.write.Number(13, i+1,BaseUtil.beDouble(base.gethP())));
			 sheet.addCell(new jxl.write.Number(14, i+1,BaseUtil.beDouble(base.getBonus())));
			 sheet.addCell(new jxl.write.Number(15, i+1,BaseUtil.beDouble(base.getGross())));
			 sheet.addCell(new jxl.write.Number(16, i+1,BaseUtil.beDouble(base.getnSSA())));
			 sheet.addCell(new jxl.write.Number(17, i+1,BaseUtil.beDouble(base.getPaye())));
			 sheet.addCell(new jxl.write.Number(18, i+1,BaseUtil.beDouble(base.getaIDS())));
			 sheet.addCell(new jxl.write.Number(19, i+1,BaseUtil.beDouble(base.getnEC())));
			 sheet.addCell(new jxl.write.Number(20, i+1,BaseUtil.beDouble(base.getTotal())));
			 sheet.addCell(new jxl.write.Number(21, i+1,BaseUtil.beDouble(base.getNet())));
		}
		book.write();
		book.close();
		return true;
	}
	public static void main(String[] args) {
	try {
			// 打开文件
			WritableWorkbook book = Workbook.createWorkbook(new File("c://test.xls"));
			// 生成名为“第一页”的工作表，参数0表示这是第一页
			WritableSheet sheet = book.createSheet(" 第一页 ", 0);
			// 在Label对象的构造子中指名单元格位置是第一列第一行(0,0)
			// 以及单元格内容为test
			Label label = new Label(0, 0, " test ");

			// 将定义好的单元格添加到工作表中
			sheet.addCell(label);

			/*
			 * 生成一个保存数字的单元格 必须使用Number的完整包路径，否则有语法歧义 单元格位置是第二列，第一行，值为789.123
			 */
			jxl.write.Number number = new jxl.write.Number(1, 0, 555.12541);
			sheet.addCell(number);

			// 写入数据并关闭文件
			book.write();
			book.close();
	} catch (Exception e) {
		System.out.println(e);
	}	

	}

}
